unit generador2u;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, ExtCtrls,
  StdCtrls, MaskEdit, ComCtrls, Grids, Interfaces;

type

  { TForm1 }

  TForm1 = class(TForm)
    Button1: TButton;
    Edit1: TEdit;
    Edit2: TEdit;
    Edit3: TEdit;
    GroupBox1: TGroupBox;
    GroupBox2: TGroupBox;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    Label6: TLabel;
    MaskEdit1: TMaskEdit;
    Memo1: TMemo;
    RadioButton1: TRadioButton;
    RadioButton2: TRadioButton;
    RadioButton3: TRadioButton;
    RadioButton4: TRadioButton;
    RadioButton5: TRadioButton;
    RadioButton6: TRadioButton;
    procedure Button1Click(Sender: TObject);
    procedure RadioButton2Change(Sender: TObject);
    procedure RadioButton4Change(Sender: TObject);
    procedure RadioButton5Change(Sender: TObject);
    procedure RadioButton6Change(Sender: TObject);

  private
    { private declarations }
  public
    { public declarations }
  end; 

var
  Form1: TForm1;

implementation

{$R *.lfm}

{ TForm1 }

procedure TForm1.RadioButton2Change(Sender: TObject);
begin
     RadioButton4.Enabled := not RadioButton2.Checked;
     if RadioButton4.checked then
        RadioButton5.checked := true;
end;

procedure TForm1.RadioButton4Change(Sender: TObject);
begin
     Label2.Enabled := RadioButton4.Checked;
     Edit1.Enabled := RadioButton4.Checked;
     Label3.Enabled := RadioButton4.Checked;
     Edit2.Enabled := RadioButton4.Checked;
     Label4.Enabled := not RadioButton4.Checked;
     Edit3.Enabled := not RadioButton4.Checked;
     Label5.Enabled := not RadioButton4.Checked;
     Memo1.Enabled := not RadioButton4.Checked;
end;

procedure TForm1.RadioButton5Change(Sender: TObject);
begin
     Label2.Enabled := RadioButton5.Checked;
     Edit1.Enabled := RadioButton5.Checked;
     Label3.Enabled := RadioButton5.Checked;
     Edit2.Enabled := RadioButton5.Checked;
     Label4.Enabled := RadioButton5.Checked;
     Edit3.Enabled := RadioButton5.Checked;
     Label5.Enabled := not RadioButton5.Checked;
     Memo1.Enabled := not RadioButton5.Checked;
end;

procedure crearEnteros(nombre:string; tipo:char; inf, sup, cant: integer; var memo:TMemo);
var i: smallint;
    arch: File of smallint;
    s: string;
    nro: string;
begin
     assign(arch, nombre+'.dat');
     rewrite(arch);

     case tipo of
     's': for i := inf to sup do
              write(arch,i);
     'a': begin
              randomize;
              for i := 1 to cant do
                  write(arch,random(sup-inf+1)+inf);
          end;
     'm': begin
               for i := 0 to memo.Lines.Count-1 do
                   begin
                        s := memo.Lines[i];
                        while Length(s) > 0 do
                        begin
                                s := trimleft(s);
                                nro := '';
                                while (Length(s) > 0) and (s[1] <> ' ') and (ord(s[1]) <> 13) and (ord(s[1]) <> 10) do
                                begin
                                     nro := nro + s[1];
                                     s := copy(s,2,length(s)-1);
                                end;
                                if nro <> '' then
                                   write(arch,strtoint(nro));
                        end;
                   end;
          end;
     end;

     memo.Text := '';
     reset(arch);
     while not eof(arch) do
     begin
          read(arch,i);
          memo.Text := memo.Text + ' ' + inttostr(i);
     end;
     memo.Text := trimleft(memo.Text);
     close(arch);

end;

procedure crearReales(nombre:string; tipo:char; inf, sup, cant: integer; var memo:TMemo);
var i: integer;
    arch: File of Real;
    r: real;
    s: string;
    nro: string;
begin
     assign(arch, nombre+'.dat');
     rewrite(arch);

     case tipo of
     'a': begin
              randomize;
              for i := 1 to cant do
              begin
                  r := (random(sup-inf)+inf)+random;
                  write(arch,(random(sup-inf)+inf)+random);
              end;
          end;
     'm': begin
               for i := 0 to memo.Lines.Count-1 do
                   begin
                        s := memo.Lines[i];
                        while Length(s) > 0 do
                        begin
                                s := trimleft(s);
                                nro := '';
                                while (Length(s) > 0) and (s[1] <> ' ') and (ord(s[1]) <> 13) and (ord(s[1]) <> 10) do
                                begin
                                     nro := nro + s[1];
                                     s := copy(s,2,length(s)-1);
                                end;
                                try
                                   if nro <> '' then
                                      write(arch,strToFloat(nro));
                                except
                                on E: Exception do
                                  begin
                                       ShowMessage('Error al convertir alguno de los parámetros a Real. Verifique el punto o coma decimal.')
                                  end;
                                end;

                        end;
                   end;
          end;
     end;

     memo.Text := '';
     reset(arch);
     while not eof(arch) do
     begin
          read(arch,r);
          memo.Text := memo.Text + ' ' + FloatToStr(r);
     end;
     memo.Text := trimleft(memo.Text);
     close(arch);

end;

procedure crearChar(nombre:string; tipo:char; inf, sup, cant: integer; var memo:TMemo);
var i: integer;
    arch: File of char;
    c: char;
    s: string;
    nro: string;
begin
     assign(arch, nombre+'.dat');
     rewrite(arch);

     case tipo of
     's': for i := inf to sup do
              write(arch,chr(i));
     'a': begin
              randomize;
              for i := 1 to cant do
                  write(arch,chr(random(sup-inf+1)+inf));
          end;
     'm': begin
               s := memo.text;
               while Length(s) > 0 do
               begin
                    nro := leftstr(s,1);
                    write(arch,nro[1]);
                    s := copy(s,2,length(s)-1);
               end;
          end;
     end;

     memo.Text := '';
     reset(arch);
     while not eof(arch) do
     begin
          read(arch,c);
          memo.Text := memo.Text + c;
     end;
     close(arch);
end;

procedure TForm1.Button1Click(Sender: TObject);
var tipo:char;
    inf, sup, cant: integer;
    s:string;
    falla:boolean;
begin

     falla := false;
     if RadioButton4.checked then       // Secuencial
        if (Edit1.Text <> '') and (Edit2.text <> '') then
           tipo := 's'
        else
           falla := true
     else
         if RadioButton5.checked then   // Aleatorio
            if (Edit1.Text <> '') and (edit2.text <> '') and (edit3.text <> '') then
               tipo := 'a'
            else
                falla := true
         else                           // Manual
            if Memo1.Text <> '' then
               tipo := 'm'
            else
                falla := true;

     if falla then
        ShowMessage('Falta completar alguno de los parámetros necesarios.')
     else
         begin
             if Edit3.Text = '' then cant := 0 else cant := StrToInt(Edit3.Text);

             if (tipo = 'm') then
                if RadioButton3.checked then         // Char
                   crearChar(MaskEdit1.text, tipo, 0, 0, 0, memo1)
                else
                   if RadioButton2.checked then     // Reales
                      crearReales(MaskEdit1.text, tipo, 0, 0, 0, memo1)
                   else                             // Enteros
                      crearEnteros(MaskEdit1.text, tipo, 0, 0, 0, memo1)
             else  // secuencia o aleatorio
               if RadioButton3.checked then         // Char
                  begin
                       s := leftstr(trimleft(Edit1.Text),1);
                       if Edit1.Text = '' then inf := 0 else inf := ord(s[1]);
                       s := leftstr(trimleft(Edit2.Text),1);
                       if Edit2.Text = '' then sup := 0 else sup := ord(s[1]);
                       if inf > sup then
                          ShowMessage('El mínimo debe ser un caracter inferior al máximo.')
                       else
                          crearChar(MaskEdit1.text, tipo, inf, sup, cant, memo1)
                  end
              else
                 begin
                      try
                         if Edit1.Text = '' then inf := 0 else inf := StrToInt(Edit1.Text);
                         if Edit2.Text = '' then sup := 0 else sup := StrToInt(Edit2.Text);
                         if inf > sup then
                            ShowMessage('El mínimo debe ser un caracter inferior al máximo.')
                         else
                           if RadioButton2.checked then     // Reales
                              crearReales(MaskEdit1.text, tipo, inf, sup, cant, memo1)
                           else                             // Enteros
                              crearEnteros(MaskEdit1.text, tipo, inf, sup, cant, memo1)
                      except
                       on E: Exception do
                       begin
                          ShowMessage('Error al convertir alguno de los parámetros a entero.')
                       end;
                      end;
                 end;
         end;
end;

procedure TForm1.RadioButton6Change(Sender: TObject);
begin
     Label2.Enabled := not RadioButton6.Checked;
     Edit1.Enabled := not RadioButton6.Checked;
     Label3.Enabled := not RadioButton6.Checked;
     Edit2.Enabled := not RadioButton6.Checked;
     Label4.Enabled := not RadioButton6.Checked;
     Edit3.Enabled := not RadioButton6.Checked;

     Label5.Enabled := RadioButton6.Checked;
     Memo1.Enabled := RadioButton6.Checked;
end;

end.

